package com.red.wood.module.eval.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.eval.dao.EvalPassEnterpriseDao;

@Repository
public class EvalPassEnterpriseDaoImpl implements EvalPassEnterpriseDao{

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Map<String, Object> getPageData(IMap param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		list = listPageData(param);
		count = listCount(param);
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}

	public List<Map<String, Object>> listPageData(IMap param){
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		StringBuffer sql = new StringBuffer();
		sql.append("select enterprise_name,form_number,apply_business,registered_address,production_unit,bureau_address,link_bandwidth,house_node_num,link_num,daytime,singleTaskId,form_type,");
		sql.append(" case when form_statue = 7 then '评测通过' when form_statue = 8 then '评测不通过' end as form_statue");
		sql.append(" from t_eval_pass_enterprise");
		sql.append(getQueryConf(param));
		sql.append(" order by form_number desc");
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	public String listCount(IMap param) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(1) count");
		sql.append(" from t_eval_pass_enterprise");
		sql.append(getQueryConf(param));
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public StringBuffer getQueryConf(IMap param) {
		String enterprise_name = CommonUtils.valueOf(param.get("enterprise_name"));
		String registered_address = CommonUtils.valueOf(param.get("registered_address"));
		String form_number = CommonUtils.valueOf(param.get("form_number"));
		String form_statue = CommonUtils.valueOf(param.get("form_statue"));
		String production_unit = CommonUtils.valueOf(param.get("production_unit"));
		String bureau_address = CommonUtils.valueOf(param.get("bureau_address"));
		String apply_business = CommonUtils.valueOf(param.get("apply_business"));
		
		StringBuffer sql = new StringBuffer();
		sql.append(" where form_statue != 0 ");
		
		if(StringUtils.isNotBlank(enterprise_name)) {
			sql.append(" and enterprise_name like '%"+enterprise_name+"%'");
		}
		if(StringUtils.isNotBlank(registered_address)) {
			sql.append(" and registered_address like '%"+registered_address+"%'");
		}
		if(StringUtils.isNotBlank(form_number)) {
			sql.append(" and form_number like '%"+form_number+"%'");
		}
		if(!StringUtils.equals(form_statue, "all")) {
			sql.append(" and form_statue like '%"+form_statue+"%'");
		}
		if(StringUtils.isNotBlank(production_unit)) {
			sql.append(" and production_unit like '%"+production_unit+"%'");
		}
		if(StringUtils.isNotBlank(bureau_address)) {
			sql.append(" and bureau_address like '%"+bureau_address+"%'");
		}
		if(StringUtils.isNotBlank(apply_business)) {
			sql.append(" and apply_business like '%"+apply_business+"%'");
		}
		return sql;
	}
}
